<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/12 0012
 * Time: 下午 12:04
 */

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

class LessonFilter extends BasicAdmin
{
    public $table = 'LessonFilter';

    public function index(){
        $this->title = '筛选条件';
        $db = Db::name($this->table)->where(['is_deleted'=>1])->order('sort asc,id asc');
        return parent::_list($db,false);
    }

    protected function _index_data_filter(&$data)
    {
        foreach ($data as &$vo) {
            $vo['value'] = trim($vo['value'],'\[\]');
        }
    }

    /**添加
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(){
        return $this->_form($this->table, 'form');
    }

    /**编辑
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(){
        return $this->_form($this->table, 'form');
    }

    /**属性值数组和json的转换
     * @param $vo
     */
    protected function _form_filter(&$vo){
        if ($this->request->isGet()){
            empty($vo) ?: $vo['value'] = json_decode($vo['value'],true);
        }elseif ($this->request->isPost()){
            $vo['value'] = json_encode($vo['value'],320);
        }
    }

    /**
     * 分类禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("分类禁用成功！", '');
        }
        $this->error("分类禁用失败，请稍候再试！");
    }

    /**
     * 分类启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("分类启用成功！", '');
        }
        $this->error("分类启用失败，请稍候再试！");
    }

    /**
     * 删除分类
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("分类删除成功！", '');
        }
        $this->error("分类删除失败，请稍候再试！");
    }

}